const BaseReducer = (apiConfig = {}) => {
  const initialState = Object.assign(
    {
      isLoading: true,
      data: "",
      hasMore: true,
      message: "",
      params: {},
      state: ""
    },
    apiConfig.initialState
  );

  return function(state, action) {
    let overwrite = (action.params || {})._overwrite;
    let currentPage;
    let data;
    let hasMore;

    switch (action.type) {
      case constantize(`REQUEST_${apiConfig.state}`):
        data = overwrite ? initialState.data : state.data;
        return Object.assign({}, state, {
          isLoading: true,
          data
        });
      case constantize(`RECIEVE_${apiConfig.state}`): {
        data = smartConcat(
          state.data,
          action.data,
          overwrite !== undefined ? overwrite : true
        );

        return Object.assign({}, state, {
          isLoading: false,
          data,
          hasMore: hasMore(),
          message: action.message,
          params: action.params,
          state: apiConfig.state
        });
      }
      default:
        return state || initialState;
    }
  };
};
export default BaseReducer;
